<?php


namespace Pzhrs\Controller;


class PzhCommonController
{
    // 测试平台的接入参数 --- start
//    const SYS_CODE = "PzhGgfw";

//    const CHANNEL_NAME = "攀枝花公共服务";
    // 测试平台的接入参数 --- end

    // 正式平台的接入参数 --- start todo 上线之前要解注
    const SYS_CODE = "PzhGzh";

    const CHANNEL_NAME = "攀枝花公众号";
    // 正式平台的接入参数 --- end

    // 互联网测试环境地址
//    const API_URL = "http://119.6.84.89:28006/ScrsGgfwService/inf/api.do";

    // 互联网正式环境地址
//    const API_URL = "http://119.6.84.89:8601/ScrsGgfwService/inf/api.do";

    // 专网正式环境
    const API_URL = "http://172.16.0.2:20385/ScrsGgfwService/inf/api.do";

    // 测试环境的私钥
//    const PRIVATE_KEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKxutfHqFmXtKHuKWOMBcs+FfQl+LxdA1wXJuOpoGPnq+9VGEgVmidFLScabhKlucjOywctBefV9O6J1Aj3Ff69TbT18FzHGFhsxBV9n3ztX3qwnwCqQBU9n7EW2dlEXWLa+x3YJWz+fv1SVfm3UZAPz2c0LPQQpRzyqLrZUJyD7AgMBAAECgYBFOo3B+oHTmkvOjdTyDvrRZn60+PxfEaFCqaKcOf8NF9LT5bYGhQyAAwIzwuyYOceQCgzUVg1pCkaPCvtDYa/JQzoOOSAqjgCjo4JV9EhkQZ5qg2VyZcU7m9uNCZoWkiIFqSCbGY5l+Jl+2IXj8uMWBNNAndw5CK1MpWhXuK+7AQJBAOLMxGzldiExOX2vzBKwxACld+fA9CUzO2gDiKCEmAfG8i+EdA+jzw9M7edvG3oqJrje5JUGhJv2F/qwN7fRYCsCQQDCogP22PcM2dAlsaPs0hIOu52fCLrcipyq69fjg/k72hQnhjdZcHVmV91b5crBhdKRXWT8viz/wD19FhFR7gpxAkEAh8c1NUhuX1K7624Ki4n93bEyRrGyuG6iexPagNbAua82IJJ4dUCJXsychPh0h7+ibP0r9bfwPfqnwnIq+UZE/wJAAq6GzXTp2oR5yLss3gt68jcFkfZl1LoYNIaxX9ImsftsQasowhO5mmJli2mcJJR6dURLKuxX5GI6tqmCLZXREQJAGt2ad+eJFl+oWeWMHktIoholFYd41uFCRDIybZDoD1qtybmWJW0jq4iNFAKSv2KdBZoPjIT78h4Evrcjc/IQxw==";
    // 测试环境的服务器公钥
//    const PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC6Yp7i1otL0vowUTrFpCbrTXKy09L6zp4VpNWsCsU4daddfGzkCA5nr/7rt5to77qXx0cEGafqCMNHe9ru4cDjbmjq6MUFzlRB0ezI8RXs1MiewGh3nRPufdKid8uPzWGo4VAGZkv2keum+GT2W5OVBnaHiZMtLQiaSMXXHNwcawIDAQAB";

    // 正式环境的私钥
//    const PRIVATE_KEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKUjyqKAJJQZrmyVy75xNDWlBb1Ndq+truXC9X2vkkR7vjRmXG8qEeVP+Y2cgYw4JdZ4+movZ5hYb9PEJc3IY4TCmZXbsdrrl/X06Xwh1AIzinGiumw+1WxOB4/evU5BHNPZhKMnwOu6crrBnVFaoe6Gv4RuLYJ9zce+vm0BZqQdAgMBAAECgYBMGGUMYk34rKjSQmhjNhjfy04MwAA7zaW1HBQxOVEGG5s23CAl3VXnu+V7mq2bZlgRLyf8/CEZc078XvsoivlH+yig9/bdyHhSZXXZ0I1Y4ytHpR5PJ19yjT6CLfAPiW8dTlRO36T43v1mm985/Nbz3ipe0QxIhJcxtmDR9MV3IQJBANRoUqL1rfVdHTeyLrwyXdee4pkXfehdiwqQUR4hHyAayM5JzHs27kiez0QFdQSRgHkqhd7l3YKKvr6R8yS8QPkCQQDHCBI+j2suUECguxoba+bmYjs0RtMyYEwaYqAOqB/S1clRL6fSSfMaV4E+qRzfLosEsACokPMbhzSX86SH22lFAkAhnNyu4xgDQxfzj2Pr7kCBdahAN8J5+gudwVbE4NLvHq32TyFnwjqVDWSZTA/HiEr/YaZLrq6OEOy8dZY3kv7pAkEAr4N1m0ecC+ZnZ5Y3apEXuHhNn0cJvXXgBrCMVwB5t9IhSv/MbYwa3lLmPZyiQknn/AU9P2k7iT4M6ibII6ogRQJAFm30oFGSHT6N5ToNIUJzw2uqRkOoiW4p3D9AZk4R8AZiHffSofymVywYvtenQ7F7zFkMns4RIhcyvJRr7hBiqA==";
    // 正式环境的服务器公钥
//    const PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC6Yp7i1otL0vowUTrFpCbrTXKy09L6zp4VpNWsCsU4daddfGzkCA5nr/7rt5to77qXx0cEGafqCMNHe9ru4cDjbmjq6MUFzlRB0ezI8RXs1MiewGh3nRPufdKid8uPzWGo4VAGZkv2keum+GT2W5OVBnaHiZMtLQiaSMXXHNwcawIDAQAB";

    const JY_CHANNEL = "攀枝花公众号";

    const JY_CHANNEL_CODE = "510400";

    const SPECIAL_PARAMS = "I01020";

    const MAX_ENCRYPT_BLOCK = 117;

    const ENV_DEVELOPMENT = "development";

    const ENV_PRODUCTION = "production";

    const ENVIRONMENT = self::ENV_PRODUCTION; // 开发环境为 development; 发布环境为 production

    public static $environmentConfig = [
        self::ENV_DEVELOPMENT => [
            // 平台的接入参数
            'sys_code' => 'PzhGgfw',
            // 渠道名称
            'channel_name' => '攀枝花公共服务',
            // 接口地址
            'api_url' => 'http://119.6.84.89:28006/ScrsGgfwService/inf/api.do',
            // 测试公钥
            'public_key' => 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC6Yp7i1otL0vowUTrFpCbrTXKy09L6zp4VpNWsCsU4daddfGzkCA5nr/7rt5to77qXx0cEGafqCMNHe9ru4cDjbmjq6MUFzlRB0ezI8RXs1MiewGh3nRPufdKid8uPzWGo4VAGZkv2keum+GT2W5OVBnaHiZMtLQiaSMXXHNwcawIDAQAB',
            // 测试私钥
            'private_key' => 'MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKxutfHqFmXtKHuKWOMBcs+FfQl+LxdA1wXJuOpoGPnq+9VGEgVmidFLScabhKlucjOywctBefV9O6J1Aj3Ff69TbT18FzHGFhsxBV9n3ztX3qwnwCqQBU9n7EW2dlEXWLa+x3YJWz+fv1SVfm3UZAPz2c0LPQQpRzyqLrZUJyD7AgMBAAECgYBFOo3B+oHTmkvOjdTyDvrRZn60+PxfEaFCqaKcOf8NF9LT5bYGhQyAAwIzwuyYOceQCgzUVg1pCkaPCvtDYa/JQzoOOSAqjgCjo4JV9EhkQZ5qg2VyZcU7m9uNCZoWkiIFqSCbGY5l+Jl+2IXj8uMWBNNAndw5CK1MpWhXuK+7AQJBAOLMxGzldiExOX2vzBKwxACld+fA9CUzO2gDiKCEmAfG8i+EdA+jzw9M7edvG3oqJrje5JUGhJv2F/qwN7fRYCsCQQDCogP22PcM2dAlsaPs0hIOu52fCLrcipyq69fjg/k72hQnhjdZcHVmV91b5crBhdKRXWT8viz/wD19FhFR7gpxAkEAh8c1NUhuX1K7624Ki4n93bEyRrGyuG6iexPagNbAua82IJJ4dUCJXsychPh0h7+ibP0r9bfwPfqnwnIq+UZE/wJAAq6GzXTp2oR5yLss3gt68jcFkfZl1LoYNIaxX9ImsftsQasowhO5mmJli2mcJJR6dURLKuxX5GI6tqmCLZXREQJAGt2ad+eJFl+oWeWMHktIoholFYd41uFCRDIybZDoD1qtybmWJW0jq4iNFAKSv2KdBZoPjIT78h4Evrcjc/IQxw==',

        ],
        self::ENV_PRODUCTION => [
            // 平台的接入参数
            'sys_code' => 'PzhGzh',
            // 渠道名称
            'channel_name' => '攀枝花公众号',
            // 正式接口地址
            'api_url' => 'http://172.16.0.2:20385/ScrsGgfwService/inf/api.do',
            // 正式公钥
            'public_key' => 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC6Yp7i1otL0vowUTrFpCbrTXKy09L6zp4VpNWsCsU4daddfGzkCA5nr/7rt5to77qXx0cEGafqCMNHe9ru4cDjbmjq6MUFzlRB0ezI8RXs1MiewGh3nRPufdKid8uPzWGo4VAGZkv2keum+GT2W5OVBnaHiZMtLQiaSMXXHNwcawIDAQAB',
            // 正式私钥
            'private_key' => 'MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKUjyqKAJJQZrmyVy75xNDWlBb1Ndq+truXC9X2vkkR7vjRmXG8qEeVP+Y2cgYw4JdZ4+movZ5hYb9PEJc3IY4TCmZXbsdrrl/X06Xwh1AIzinGiumw+1WxOB4/evU5BHNPZhKMnwOu6crrBnVFaoe6Gv4RuLYJ9zce+vm0BZqQdAgMBAAECgYBMGGUMYk34rKjSQmhjNhjfy04MwAA7zaW1HBQxOVEGG5s23CAl3VXnu+V7mq2bZlgRLyf8/CEZc078XvsoivlH+yig9/bdyHhSZXXZ0I1Y4ytHpR5PJ19yjT6CLfAPiW8dTlRO36T43v1mm985/Nbz3ipe0QxIhJcxtmDR9MV3IQJBANRoUqL1rfVdHTeyLrwyXdee4pkXfehdiwqQUR4hHyAayM5JzHs27kiez0QFdQSRgHkqhd7l3YKKvr6R8yS8QPkCQQDHCBI+j2suUECguxoba+bmYjs0RtMyYEwaYqAOqB/S1clRL6fSSfMaV4E+qRzfLosEsACokPMbhzSX86SH22lFAkAhnNyu4xgDQxfzj2Pr7kCBdahAN8J5+gudwVbE4NLvHq32TyFnwjqVDWSZTA/HiEr/YaZLrq6OEOy8dZY3kv7pAkEAr4N1m0ecC+ZnZ5Y3apEXuHhNn0cJvXXgBrCMVwB5t9IhSv/MbYwa3lLmPZyiQknn/AU9P2k7iT4M6ibII6ogRQJAFm30oFGSHT6N5ToNIUJzw2uqRkOoiW4p3D9AZk4R8AZiHffSofymVywYvtenQ7F7zFkMns4RIhcyvJRr7hBiqA==',

        ]
    ];



    /**
     * 构造函数
     */
    function __construct()
    {
        header('Content-Type:application/json; charset=utf-8');
        if (!method_exists($this, ACTION_NAME)) {
            $function = ACTION_NAME;
            A('Receive')->$function();
        }
    }

    /**
     * 2.1.1.城乡居民社会保险个人权益查询 | 输入社会保障号（身份证号码）、年度、参保地行政区划代码、姓名、险种查询城乡居民个人权益信息。
     */
    public function SCGRQY02()
    {
        if (empty($_POST['aac002'])) {
            echo $this->buildResponse(400, [], '未获取到身份证信息');
            return;
        }
        if (empty($_POST['aae001'])) {
            echo $this->buildResponse(400, [], '未获取到年度信息');
            return;
        }
        if (empty($_POST['aab301'])) {
            echo $this->buildResponse(400, [], '未获取到参保地行政区划代码信息');
            return;
        }
        if (empty($_POST['aac003'])) {
            echo $this->buildResponse(400, [], '未获取到姓名信息');
            return;
        }
        if (empty($_POST['aaz002'])) {
            echo $this->buildResponse(400, [], '未获取到业务序列号信息');
            return;
        }
        if (empty($_POST['aae140'])) {
            echo $this->buildResponse(400, [], '未获取到险种信息,说明：120机关养老，180年金');
            return;
        }
        $ori_params = $_POST;
        $result = $this->processOnRequest('SCGRQY02', 'I01003', $ori_params);
        if (isset($result['code'])) {
            echo $this->buildResponse($result['code'], [], $result['msg']);
            return;
        }
        echo $this->buildResponse(200, $result);
    }

    /**
     * 2.1.2.机关保险个人权益查询 | 输入社会保障号（身份证号码）、年度、参保地行政区划代码、姓名、险种查询城乡居民个人权益信息。
     */
    public function SCGRQY01()
    {
        if (empty($_POST['aac002'])) {
            echo $this->buildResponse(400, [], '未获取到身份证信息');
            return;
        }
        if (empty($_POST['aac003'])) {
            echo $this->buildResponse(400, [], '未获取到姓名信息');
            return;
        }
        $ori_params = $_POST;
        $ori_params['aae001'] = date('Y');
        $ori_params['aaz002'] = "";
        $ori_params['aab301'] = "510400";
        $ori_params['aae140'] = "";
        $result = $this->processOnRequest(__FUNCTION__, 'I01003', $ori_params);
        if (isset($result['code'])) {
            echo $this->buildResponse($result['code'], [], $result['msg']);
            return;
        }
        if (!isset($result['CbLists'])) {
            echo $this->buildResponse(400, [], '未获取到CbLists数据块信息');
            return;
        }
        $cbList = $result['CbLists'];
        if (!isset($cbList['cbList'])) {
            echo $this->buildResponse(400, [], '未获取到cbLists数据块信息');
            return;
        }
        if (!isset($result['JFLists'])) {
            echo $this->buildResponse(400, [], '未获取到JFLists数据块信息');
            return;
        }
        $jfLists = $result['JFLists'];
        if (!isset($jfLists['DnjfList'])) {
            echo $this->buildResponse(400, [], '未获取到DnjfList数据块信息');
            return;
        }
        $rebuildList = $this->doMergeData($cbList['cbList'], $jfLists['DnjfList']);
//        $this->logCommonFile(__FUNCTION__, 'doMergeData', json_encode($rebuildList));
        echo $this->buildResponse(200, ['rows' => $rebuildList]);
    }

    /**
     * 2.1.3.企业职工保险个人权益查询 | 输入社会保障号（身份证号码）、年度、参保地行政区划代码、姓名、险种查询企业职工个人权益信息。
     */
    public function SCGRQY03()
    {
        if (empty($_POST['aac002'])) {
            echo $this->buildResponse(400, [], '未获取到身份证信息');
            return;
        }
        if (empty($_POST['aab301'])) {
            echo $this->buildResponse(400, [], '未获取到参保地行政区划代码信息');
            return;
        }
        if (empty($_POST['aac003'])) {
            echo $this->buildResponse(400, [], '未获取到姓名信息');
            return;
        }
        $ori_params = $_POST;
        $ori_params['aae001'] = date('Y');
        $ori_params['aaz002'] = '';
        if (empty($_POST['aae140'])) {
            $ori_params['aae140'] = '110';
        }
        $result = $this->processOnRequest(__FUNCTION__, 'I01003', $ori_params);
        if (isset($result['code']) && intval($result['code'] != 1)) {
            echo $this->buildResponse($result['code'], [], $result['msg']);
            return;
        }
        if (!isset($result['CbLists'])) {
            echo $this->buildResponse(400, [], '未获取到CbLists数据块信息');
            return;
        }
        $cbList = $result['CbLists'];
        if (!isset($cbList['cbList'])) {
            echo $this->buildResponse(400, [], '未获取到cbLists数据块信息');
            return;
        }
        if (!isset($result['JFLists'])) {
            echo $this->buildResponse(400, [], '未获取到JFLists数据块信息');
            return;
        }
        $JFLists = $result['JFLists'];
        if (!isset($JFLists['DnjfList'])) {
            echo $this->buildResponse(400, [], '未获取到DnjfList数据块信息');
            return;
        }
        $rebuildList = $this->doMergeData($cbList['cbList'], $JFLists['DnjfList']);
//        $this->logCommonFile(__FUNCTION__, 'doMergeData', json_encode($rebuildList));
        echo $this->buildResponse(200, ['rows' => $rebuildList]);
    }

    /**
     * 2.1.4.省机保参保信息查询（总集社保） | 通过身份证号码查询省机保参保信息。
     * todo 测试通过
     * aac002 511121198204160036
     */
    public function CX016JG()
    {
        if (empty($_POST['aac002'])) {
            echo $this->buildResponse(400, [], '未获取到身份证信息');
            return;
        }
        $ori_params = $_POST;
        $result = $this->processOnRequest('CX016-JG', 'I01020', $ori_params);
        if (isset($result['code']) && intval($result['code'] != 1)) {
            echo $this->buildResponse($result['code'], [], $result['msg']);
            return;
        }
        if (!isset($result['return_xml'])) {
            echo $this->buildResponse(400, [], '未获取到return_xml数据块信息');
            return;
        }
        $returnXml = $result['return_xml'];
        if (!isset($returnXml['output'])) {
            echo $this->buildResponse(400, [], '未获取到output数据块信息');
            return;
        }
        echo $this->buildResponse(200, $returnXml['output']);
    }

    /**
     * 2.1.5.机关事业单位养老保险缴费明细查询（总集社保） | 通过机保个人编号查询机关事业单位养老保险或职业年金缴费明细。 缴费开始期号
     * todo 测试 result [省机关养老保险]未获取到任何信息
     */
    public function CX013JG()
    {
        if (empty($_POST['aac001'])) {
            echo $this->buildResponse(400, [], '未获取到个人编码信息');
            return;
        }
        if (empty($_POST['aae140'])) {
            echo $this->buildResponse(400, [], '未获取到险种信息，说明：120机关养老，180年金');
            return;
        }
        if (empty($_POST['aae041'])) {
            echo $this->buildResponse(400, [], '未获取到缴费开始期号[yyyymm]信息');
            return;
        }
        if (empty($_POST['aae042'])) {
            echo $this->buildResponse(400, [], '未获取到缴费截止期号[yyyymm]信息');
            return;
        }
        $ori_params = $_POST;
        $result = $this->processOnRequest('CX013-JG', 'I01020', $ori_params);
        if (isset($result['code']) && intval($result['code']) != 1) {
            echo $this->buildResponse($result['code'], [], $result['msg']);
            return;
        }
        if (!isset($result['return_xml'])) {
            echo $this->buildResponse(400, [], '未获取到return_xml数据块信息');
            return;
        }
        $returnXml = $result['return_xml'];
        if (!isset($returnXml['output'])) {
            echo $this->buildResponse(400, [], '未获取到output数据块信息');
            return;
        }
        echo $this->buildResponse(200, $returnXml['output']);
    }

    /**
     * 2.1.6 机关事业单位养老金查询（总集社保) | 通过机保个人编号查询机关事业单位养老金明细
     * todo 未测试
     */
    public function YTJ015()
    {
        if (empty($_POST['aac001'])) {
            echo $this->buildResponse(400, [], '未获取到个人编码信息');
            return;
        }
        if (empty($_POST['aae041'])) {
            echo $this->buildResponse(400, [], '未获取到缴费开始期号[yyyymm]信息');
            return;
        }
        if (empty($_POST['aae042'])) {
            echo $this->buildResponse(400, [], '未获取到缴费截止期号[yyyymm]信息');
            return;
        }
        if (empty($_POST['startrow'])) {
            echo $this->buildResponse(400, [], '未获取到开始条数信息');
            return;
        }
        if (empty($_POST['endrow'])) {
            echo $this->buildResponse(400, [], '未获取到截止条数信息');
            return;
        }
        $ori_params = $_POST;
        $result = $this->processOnRequest('YTJ015', 'I01020', $ori_params);
        if (isset($result['code']) && intval($result['code']) != 1) {
            echo $this->buildResponse($result['code'], [], $result['msg']);
            return;
        }
        if (!isset($result['return_xml'])) {
            echo $this->buildResponse(400, [], '未获取到return_xml数据块信息');
            return;
        }
        $returnXml = $result['return_xml'];
        if (!isset($returnXml['output'])) {
            echo $this->buildResponse(400, [], '未获取到output数据块信息');
            return;
        }
        echo $this->buildResponse(200, $returnXml['output']);
    }

    /**
     * 2.1.7.企保参保信息查询（总集社保） | 通过身份证号码查询省本级社保参保信息
     * todo 测试通过
     */
    public function PRCCX016()
    {
        $identifyNo = $_POST['aac002'];
        if (empty($identifyNo)) {
            echo $this->buildResponse(400, [], '未获取到身份证信息');
            return;
        }
        $ori_params = [
            'aac002' => empty($identifyNo) ? '510302197606280045' : $identifyNo,
        ];
        $result = $this->processOnRequest('PRC-CX016', 'I01020', $ori_params);
        if (!isset($result['return_xml'])) {
            echo $this->buildResponse(400, [], '未获取到return_xml数据块信息');
            return;
        }
        $returnXml = $result['return_xml'];
        if (!isset($returnXml['output'])) {
            echo $this->buildResponse(400, [], '未获取到output数据块信息');
            return;
        }
        echo $this->buildResponse(200, $returnXml['output']);
    }

    /**
     * 2.1.8.企业职工养老保险缴费明细查询（总集社保） | 通过省本级社保个人编号查询省本级企业职工养老保险缴费明细
     * todo 测试通过 aac001 3004737835 | startrow 1 | endrow 4
     */
    public function PRCYTJ005()
    {
        if (empty($_POST['aac001'])) {
            echo $this->buildResponse(400, [], '未获取到个人编码信息');
            return;
        }
        if (empty($_POST['startrow'])) {
            echo $this->buildResponse(400, [], '未获取到开始条数信息');
            return;
        }
        if (empty($_POST['endrow'])) {
            echo $this->buildResponse(400, [], '未获取到截止条数信息');
            return;
        }
        $ori_params = $_POST;
        $result = $this->processOnRequest('PRC-YTJ005', 'I01020', $ori_params);
        if (isset($result['code']) && intval($result['code']) != 1) {
            echo $this->buildResponse($result['code'], [], $result['msg']);
            return;
        }
        if (!isset($result['return_xml'])) {
            echo $this->buildResponse(400, [], '未获取到return_xml数据块信息');
            return;
        }
        $returnXml = $result['return_xml'];
        if (!isset($returnXml['output'])) {
            echo $this->buildResponse(400, [], '未获取到output数据块信息');
            return;
        }
        echo $this->buildResponse(200, $returnXml['output']);
    }

    /**
     * 2.1.9.失业保险缴费明细查询（总集社保） | PRCYTJ020  aac001 = 3006788228
     * todo 未测试 数据返回成功 {"code":"1","message":"成功"}
     */
    public function PRCYTJ020()
    {
        if (empty($_POST['aac001'])) {
            echo $this->buildResponse(400, [], '未获取到个人编码信息');
            return;
        }
        if (empty($_POST['startrow'])) {
            echo $this->buildResponse(400, [], '未获取到开始条数信息');
            return;
        }
        if (empty($_POST['endrow'])) {
            echo $this->buildResponse(400, [], '未获取到截止条数信息');
            return;
        }

        $ori_params = $_POST;

        $result = $this->processOnRequest('PRC-YTJ020', 'I01020', $ori_params);
        if (isset($result['code']) && intval($result['code']) != 1) {
            echo $this->buildResponse($result['code'], [], $result['msg']);
            return;
        }
        if (!isset($result['return_xml'])) {
            echo $this->buildResponse(400, [], '未获取到return_xml数据块信息');
            return;
        }
        $returnXml = $result['return_xml'];
        if (!isset($returnXml['output'])) {
            echo $this->buildResponse(400, [], '未获取到output数据块信息');
            return;
        }
        echo $this->buildResponse(200, $returnXml['output']);
    }

    /**
     * 2.1.10.工伤保险缴费明细查询（总集社保）| PRCYTJ006
     * todo 未测试  测试数据返回成功
     */
    public function PRCYTJ006()
    {
        if (empty($_POST['aac001'])) {
            echo $this->buildResponse(400, [], '未获取到个人编码信息');
            return;
        }
        if (empty($_POST['startrow'])) {
            echo $this->buildResponse(400, [], '未获取到开始条数信息');
            return;
        }
        if (empty($_POST['endrow'])) {
            echo $this->buildResponse(400, [], '未获取到截止条数信息');
            return;
        }
        $ori_params = $_POST;
        $result = $this->processOnRequest('PRC-YTJ006', 'I01020', $ori_params);
        if (isset($result['code']) && intval($result['code']) != 1) {
            echo $this->buildResponse($result['code'], [], $result['msg']);
            return;
        }
        if (!isset($result['return_xml'])) {
            echo $this->buildResponse(400, [], '未获取到return_xml数据块信息');
            return;
        }
        $returnXml = $result['return_xml'];
        if (!isset($returnXml['output'])) {
            echo $this->buildResponse(400, [], '未获取到output数据块信息');
            return;
        }
        echo $this->buildResponse(200, $returnXml['output']);
    }

    /**
     * 2.1.11.企业职工养老金查询（总集社保） | 通过省本级社保个人编号查询省本级企业职工养老发放明细。
     * todo 测试通过 aac001 116686979 | aae140 180 | startrow 1 | endrow 4
     */
    public function PRCYTJ007()
    {
        if (empty($_POST['aac001'])) {
            echo $this->buildResponse(400, [], '未获取到个人编码信息');
            return;
        }
        if (empty($_POST['aae140'])) {
            echo $this->buildResponse(400, [], '未获取到险种信息,说明：120机关养老，180职工年金');
            return;
        }
        if (empty($_POST['startrow'])) {
            echo $this->buildResponse(400, [], '未获取到开始条数信息');
            return;
        }
        if (empty($_POST['endrow'])) {
            echo $this->buildResponse(400, [], '未获取到截止条数信息');
            return;
        }
        $ori_params = $_POST;
        $result = $this->processOnRequest('PRC-YTJ007', 'I01020', $ori_params);
        if (isset($result['code']) && intval($result['code']) != 1) {
            echo $this->buildResponse($result['code'], [], $result['msg']);
            return;
        }
        if (!isset($result['return_xml'])) {
            echo $this->buildResponse(400, [], '未获取到return_xml数据块信息');
            return;
        }
        $returnXml = $result['return_xml'];
        if (!isset($returnXml['output'])) {
            echo $this->buildResponse(400, [], '未获取到output数据块信息');
            return;
        }
        echo $this->buildResponse(200, $returnXml['output']);
    }


    /**
     * 2.1.13.机关养老历年账户信息（总集社保）|
     * todo 测试通过 aac001 2000083903 | aae140 110 | startrow 1 | endrow 4
     */
    public function YTJ013JG()
    {
        if (empty($_POST['aac001'])) {
            echo $this->buildResponse(400, [], '未获取到个人编码信息');
            return;
        }
        if (empty($_POST['aae140'])) {
            echo $this->buildResponse(400, [], '未获取到险种信息,说明：120机关养老，180职工年金');
            return;
        }
        if (empty($_POST['startrow'])) {
            echo $this->buildResponse(400, [], '未获取到开始条数信息');
            return;
        }
        if (empty($_POST['endrow'])) {
            echo $this->buildResponse(400, [], '未获取到截止条数信息');
            return;
        }
        $ori_params = $_POST;
        $result = $this->processOnRequest('YTJ013-JG', 'I01020', $ori_params);
        if (isset($result['code']) && intval($result['code']) != 1) {
            echo $this->buildResponse($result['code'], [], $result['msg']);
            return;
        }
        if (!isset($result['return_xml'])) {
            echo $this->buildResponse(400, [], '未获取到return_xml数据块信息');
            return;
        }
        $returnXml = $result['return_xml'];
        if (!isset($returnXml['output'])) {
            echo $this->buildResponse(400, [], '未获取到output数据块信息');
            return;
        }
        echo $this->buildResponse(200, $returnXml['output']);
    }

    /**
     * 2.1.14.城职养老年度账户信息查询（总集社保）
     * todo 测试通过 aac001 3004737835 | aae140 110 | startrow 1 | endrow 4
     */
    public function PRCYTJ039()
    {
        if (empty($_POST['aac001'])) {
            echo $this->buildResponse(400, [], '未获取到个人编码信息');
            return;
        }
        if (empty($_POST['aae140'])) {
            echo $this->buildResponse(400, [], '未获取到险种信息,说明：120机关养老，180职工年金');
            return;
        }
        if (empty($_POST['startrow'])) {
            echo $this->buildResponse(400, [], '未获取到开始条数信息');
            return;
        }
        if (empty($_POST['endrow'])) {
            echo $this->buildResponse(400, [], '未获取到截止条数信息');
            return;
        }
        $ori_params = $_POST;
        $result = $this->processOnRequest('PRC-YTJ039', 'I01020', $ori_params);

        if (isset($result['code']) && intval($result['code']) != 1) {
            echo $this->buildResponse($result['code'], [], $result['msg']);
            return;
        }
        if (!isset($result['return_xml'])) {
            echo $this->buildResponse(400, [], '未获取到return_xml数据块信息');
            return;
        }
        $returnXml = $result['return_xml'];
        if (!isset($returnXml['output'])) {
            echo $this->buildResponse(400, [], '未获取到output数据块信息');
            return;
        }
        echo $this->buildResponse(200, $returnXml['output']);
    }

    /**
     * 2.1.15.城乡居民养老参保信息查询（总集社保）
     * todo 测试通过
     */
    public function YTJ003()
    {
        if (empty($_POST['aac002'])) {
            echo $this->buildResponse(400, [], '未获取到身份证信息');
            return;
        }
        $ori_params = $_POST;
        $result = $this->processOnRequest('YTJ003', 'I01020', $ori_params);
        if (isset($result['code']) && intval($result['code']) != 1) {
            echo $this->buildResponse($result['code'], [], $result['msg']);
            return;
        }
        if (!isset($result['return_xml'])) {
            echo $this->buildResponse(400, [], '未获取到return_xml数据块信息');
            return;
        }
        $returnXml = $result['return_xml'];
        if (!isset($returnXml['output'])) {
            echo $this->buildResponse(400, [], '未获取到output数据块信息');
            return;
        }
        echo $this->buildResponse(200, $returnXml['output']);
    }

    /**
     * 2.1.16.城乡居民养老缴费明细（总集社保）
     * todo 测试通过
     * aac001 116686979
     */
    public function YTJ038()
    {
        if (empty($_POST['aac001'])) {
            echo $this->buildResponse(400, [], '未获取到个人编码信息');
            return;
        }
        $ori_params = $_POST;
        $result = $this->processOnRequest('YTJ038', 'I01020', $ori_params);
        if (isset($result['code']) && intval($result['code']) != 1) {
            echo $this->buildResponse($result['code'], [], $result['msg']);
            return;
        }
        if (!isset($result['return_xml'])) {
            echo $this->buildResponse(400, [], '未获取到return_xml数据块信息');
            return;
        }
        $returnXml = $result['return_xml'];
        if (!isset($returnXml['output'])) {
            echo $this->buildResponse(400, [], '未获取到output数据块信息');
            return;
        }
        echo $this->buildResponse(200, $returnXml['output']);
    }

    /**
     * 2.1.17.城乡居民养老发放明细（总集社保）| JMSD004
     */
    public function JMSD004()
    {
        $ori_params = [];
        if (empty($_POST['aac001'])) {
            echo $this->buildResponse(400, [], '未获取到人员编号信息');
            return;
        }
        $ori_params['aac001'] = $_POST['aac001'];
        if (!isset($_POST['aac002'])) {
            $ori_params['aac002'] = '';
        }
        if (!isset($_POST['aac003'])) {
            $ori_params['aac003'] = '';
        }
        if (!isset($_POST['aae002'])) {
            $ori_params['aae002'] = '';
        }

        $result = $this->processOnRequest('JMSD004', 'I01020', $ori_params);
        if (isset($result['code']) && intval($result['code']) != 1) {
            echo $this->buildResponse($result['code'], [], $result['msg']);
            return;
        }
        if (!isset($result['reusltlist'])) {
            echo $this->buildResponse(400, [], '未获取到reusltlist数据块信息');
            return;
        }
        echo $this->buildResponse(200, ['rows' => $result['reusltlist']]);
    }

    /**
     * 2.1.22.养老待遇资格认证（总集社保）| RZ0001
     *
     * 测试环境 aac002 = 512531195709262080
     *
     * 测试通过
     */
    public function RZ0001()
    {
        if (empty($_POST['aac002'])) {
            echo $this->buildResponse(400, [], '未获取到身份证信息');
            return;
        }
        $ori_params = $_POST;
        $result = $this->processOnRequest('RZ0001', 'I01020', $ori_params);
        if (isset($result['code']) && intval($result['code']) != 1) {
            echo $this->buildResponse($result['code'], [], $result['msg']);
            return;
        }
        if (!isset($result['return_xml'])) {
            echo $this->buildResponse(400, [], '未获取到return_xml数据块信息');
            return;
        }
        $returnXml = $result['return_xml'];
        if (!isset($returnXml['output'])) {
            echo $this->buildResponse(400, [], '未获取到output数据块信息');
            return;
        }
        echo $this->buildResponse(200, $returnXml['output']);
    }

    /**
     * 2.1.22.养老待遇资格认证（总集社保） - 保存 | RZ0002
     */
    public function RZ0002()
    {
        if (empty($_POST['aac002'])) {
            echo $this->buildResponse(400, [], '未获取到身份证信息');
            return;
        }
        if (empty($_POST['aac003'])) {
            echo $this->buildResponse(400, [], '未获取到姓名信息');
            return;
        }
        if (empty($_POST['aae005'])) {
            echo $this->buildResponse(400, [], '未获取到联系方式信息');
            return;
        }

        $ori_params = $_POST;
        $ori_params['aae230'] = date('Ymd');
        $ori_params['aaa135'] = "50"; // {50:联网自助认证，99:其他, 22:街道社区上门认证，21：经办机构上门认证}
        $ori_params['bae135'] = "63"; // {11:面见本人，12：入户调查，13：委托他人代理，51：指纹识别，52：人像识别，53：掌纹识别，54：声波语音识别，55：虹膜识别，63：手机动态识别}
        $ori_params['aae001'] = $_POST['aae005'];
        $ori_params['aae006'] = '';
        $ori_params['aae013'] = '无';
        $ori_params['aae011'] = $_POST['aae005'];
        $ori_params['aae036'] = date('Ymd');
        $ori_params['aaa146'] = "省社保";
        $ori_params['aab301'] = "519900";

        $result = $this->processOnRequest('RZ0002', 'I01020', $ori_params);
        if (isset($result['code']) && intval($result['code']) != 1) {
            echo $this->buildResponse($result['code'], [], $result['msg']);
            return;
        }
        echo $this->buildResponse(200, $result);
    }

    /**
     * 2.1.23.人员认证信息查询（总集社保）| RZ0003
     *
     * 测试环境 aac002 = 510321196211105204
     *
     *
     */
    public function RZ0003()
    {
        if (empty($_POST['aac002'])) {
            echo $this->buildResponse(400, [], '未获取到身份证信息');
            return;
        }
        $ori_params = $_POST;
        $result = $this->processOnRequest('RZ0003', 'I01020', $ori_params);
        if (isset($result['code']) && intval($result['code']) != 1) {
            echo $this->buildResponse($result['code'], [], $result['msg']);
            return;
        }
        if (!isset($result['output'])) {
            echo $this->buildResponse(400, [], '未获取到output数据块信息');
            return;
        }
        $output = $result['output'];
        if (!isset($output['resultset'])) {
            echo $this->buildResponse(400, [], '未获取到resultset数据块信息');
            return;
        }
        echo $this->buildResponse(200, ['rows' => $output['resultset']]);
    }

    /**
     * 2.1.24.待遇人员可认证校验查询（总集社保）| RZ0004
     *
     */
    public function RZ0004()
    {
        if (empty($_POST['aac002'])) {
            echo $this->buildResponse(400, [], '未获取到身份证信息');
            return;
        }
        $ori_params = $_POST;
        $result = $this->processOnRequest('RZ0004', 'I01020', $ori_params);

        if (isset($result['code']) && intval($result['code']) != 1) {
            echo $this->buildResponse($result['code'], [], $result['msg']);
            return;
        }
        echo $this->buildResponse(200, $result);
    }

    /**
     * 2.1.31.工伤保险待遇信息查询（总集社保）| PRCIC022
     *
     */
    public function PRCIC022()
    {
        if (empty($_POST['aac002'])) {
            echo $this->buildResponse(400, [], '未获取到身份证信息');
            return;
        }
        $ori_params = $_POST;
        $result = $this->processOnRequest('PRC-IC022', 'I01020', $ori_params);
        if (isset($result['code']) && intval($result['code']) != 1) {
            echo $this->buildResponse($result['code'], [], $result['msg']);
            return;
        }
        if (!isset($result['return_xml'])) {
            echo $this->buildResponse(400, [], '未获取到return_xml数据块信息');
            return;
        }
        $returnXml = $result['return_xml'];
        if (!isset($returnXml['output'])) {
            echo $this->buildResponse(400, [], '未获取到output数据块信息');
            return;
        }
        echo $this->buildResponse(200, $returnXml['output']);
    }

    /**
     * 2.2.1 社会保障卡状态查询 | ZMZX001
     *
     *
     */
    public function ZMZX001()
    {
        if (empty($_POST['aac002'])) {
            echo $this->buildResponse(400, [], '未获取到身份证信息');
            return;
        }
        $ori_params = $_POST;
        $result = $this->processOnRequest('zmzx-001', 'I01002', $ori_params);
        if ($result['code'] != 0) {
            echo $this->buildResponse($result['code'], [], $result['msg']);
            return;
        }
        echo $this->buildResponse(200, $result);
    }

    /**
     * 2.2.2 人员基础信息查询 | ZMZX002
     * aac002 = 510182198410021012
     * 正式环境测试通过
     */
    public function ZMZX002()
    {
        if (empty($_POST['aac002'])) {
            echo $this->buildResponse(400, [], '未获取到身份证信息');
            return;
        }
        $ori_params = $_POST;
        $result = $this->processOnRequest('zmzx-002', 'I01002', $ori_params);
        if ($result['code'] != 0) {
            echo $this->buildResponse($result['code'], [], $result['msg']);
            return;
        }
        echo $this->buildResponse(200, $result);
    }

    /**
     * 2.2.3.社会保障卡挂失 | [zmzx_003]
     *
     * 正式环境 测试通过
     */
    public function ZMZX003()
    {
        if (empty($_POST['aac002'])) {
            echo $this->buildResponse(400, [], '未获取到身份证信息');
            return;
        }
        if (empty($_POST['aaz500'])) {
            echo $this->buildResponse(400, [], '未获取到社会保障卡信息');
            return;
        }
        $ori_params = $_POST;
        $ori_params['bjbm'] = sprintf("wxsbcjgs%s", time());
        $ori_params['bjname'] = "社会保障卡挂失";
        $result = $this->processOnRequest('zmzx-003', 'I01002', $ori_params);
        if ($result['code'] != 1) {
            echo $this->buildResponse($result['code'], [], $result['msg']);
            return;
        }

        echo $this->buildResponse(200, []);
    }

    /**
     * 2.2.4 社会保障卡非关键信息变更 => 保存接口服务
     *
     * 正式环境 测试通过
     */
    public function ZMZX004()
    {
        if (empty($_POST['aac002'])) {
            echo $this->buildResponse(400, [], '未获取到身份证信息');
            return;
        }
        if (empty($_POST['aac067'])) {
            echo $this->buildResponse(400, [], '未获取到手机号码信息');
            return;
        }
        if (empty($_POST['aae006'])) {
            echo $this->buildResponse(400, [], '未获取到常驻地址信息');
            return;
        }
        if (empty($_POST['aac010'])) {
            echo $this->buildResponse(400, [], '未获取到户口所在地信息');
            return;
        }
        $ori_params = $_POST;
        $ori_params['bjbm'] = sprintf("wxsbcj%s", time());
        $ori_params['bjname'] = "社会保障卡非关键信息变更";
        $result = $this->processOnRequest('zmzx-004', 'I01002', $ori_params);
        if ($result['code'] != 1) {
            echo $this->buildResponse($result['code'], [], $result['msg']);
            return;
        }
        echo $this->buildResponse(200, []);
    }

    /**
     * 2.2.5-2 社会保障卡参保关系转移
     *
     * 正式环境 测试通过
     */
    public function ZMZX0009()
    {
        if (empty($_POST['aac002'])) {
            echo $this->buildResponse(400, [], '未获取到身份证信息');
            return;
        }
        if (empty($_POST['aaz500'])) {
            echo $this->buildResponse(400, [], '未获取到社会保障卡信息');
            return;
        }
        if (empty($_POST['aae140'])) {
            echo $this->buildResponse(400, [], '未获取到险种类型信息');
            return;
        }
        if (empty($_POST['aab034'])) {
            echo $this->buildResponse(400, [], '未获取到新参保地信息');
            return;
        }

        $ori_params = $_POST;
        $ori_params['bjbm'] = sprintf("wxsbcjzy%s", time());
        $ori_params['bjname'] = "社会保障卡参保关系转移";
        $result = $this->processOnRequest('zmzx-014', 'I01002', $ori_params);
        if ($result['code'] != 0) {
            echo $this->buildResponse($result['code'], [], $result['msg']);
            return;
        }
        echo $this->buildResponse(200, $result);
    }

    /**
     * 2.2.5-1 社会保障卡参保关系转移
     *
     * 正式环境 测试通过
     */
    public function ZMZX014()
    {
        if (empty($_POST['aac002'])) {
            echo $this->buildResponse(400, [], '未获取到身份证信息');
            return;
        }
        if (empty($_POST['aaz500'])) {
            echo $this->buildResponse(400, [], '未获取到社会保障卡信息');
            return;
        }

        $ori_params = $_POST;
        $result = $this->processOnRequest('zmzx-014', 'I01002', $ori_params);
        if ($result['code'] != 0) {
            echo $this->buildResponse($result['code'], [], $result['msg']);
            return;
        }
        echo $this->buildResponse(200, $result);
    }

    /**
     * 2.2.6.社会保障卡激活 | [WZ7000]
     *
     * todo 需要测试
     */
    public function WZ7000()
    {
        if (empty($_POST['aac002'])) {
            echo $this->buildResponse(400, [], '未获取到身份证信息');
            return;
        }
        if (empty($_POST['aac003'])) {
            echo $this->buildResponse(400, [], '未获取到姓名信息');
            return;
        }
        if (empty($_POST['aaz500'])) {
            echo $this->buildResponse(400, [], '未获取到社会保障卡信息');
            return;
        }
        if (empty($_POST['aab034'])) {
            echo $this->buildResponse(400, [], '未获取到行政区划信息');
            return;
        }
        if (empty($_POST['aae435'])) {
            echo $this->buildResponse(400, [], '未获取到领卡人信息');
            return;
        }
        if (empty($_POST['aae005'])) {
            echo $this->buildResponse(400, [], '未获取到移动电话信息');
            return;
        }
        if (empty($_POST['aae036'])) {
            echo $this->buildResponse(400, [], '未获取到激活时间信息');
            return;
        }
        $ori_params = $_POST;
        $ori_params['akb020'] = '';
        $ori_params['aae011'] = '';
        $ori_params['wsjs'] = '';
        $ori_params['Atr'] = '';
        $result = $this->processOnRequest('WZ7000', 'I01002', $ori_params);
        if ($result['code'] != 1) {
            echo $this->buildResponse($result['code'], [], $result['msg']);
            return;
        }
        echo $this->buildResponse(200, $result);
    }

    /**
     * 2.2.7.  社保卡解挂 | [G400]
     *
     * 正式环境 测试通过
     */
    public function G400()
    {
        $ori_params = [
            'aac058' => '01', // 默认
            'ywlx' => '04'
        ];
        if (empty($_POST['aac002'])) {
            echo $this->buildResponse(400, [], '未获取到身份证信息');
            return;
        }
        $ori_params['aac002'] = $_POST['aac002'];
        if (empty($_POST['aac003'])) {
            echo $this->buildResponse(400, [], '未获取到姓名信息');
            return;
        }
        $ori_params['aac003'] = $_POST['aac003'];
        $ori_params['aae011'] = $_POST['aac003'];
        if (empty($_POST['aab034'])) {
            echo $this->buildResponse(400, [], '未获取到行政区划信息');
            return;
        }
        $ori_params['aab034'] = $_POST['aab034'];
        $ori_params['yae735'] = $_POST['aab034'];

        if (empty($_POST['aab301'])) {
            echo $this->buildResponse(400, [], '未获取到参保地信息');
            return;
        }
        $ori_params['aab301'] = $_POST['aab301'];
        $result = $this->processOnRequest('G400', 'I01002', $ori_params);

        if ($result['code'] != 1) {
            echo $this->buildResponse($result['code'], [], $result['msg']);
            return;
        }
        echo $this->buildResponse(200, []);
    }

    /**
     * 2.2.8 银行服务网点信息查询 | Q7100
     *  正式环境测试通过  aab034 = 510400
     *
     */
    public function Q7100()
    {
        if (empty($_POST['aab034'])) {
            echo $this->buildResponse(400, [], '未获取市州代码');
            return;
        }
        $ori_params = $_POST;
        $ori_params['aab301'] = '';
        $ori_params['aae008'] = '';
        $ori_params['startrow'] = '1';
        $ori_params['endrow'] = '500';
        $result = $this->processOnRequest('Q7100', 'I01002', $ori_params);
        echo $this->buildResponse(200, $result);
    }

    /**
     * 2.2.9.人社服务网点查询 | Q7200
     *
     * 正式环境测试通过  aab034 = 510400
     */
    public function Q7200()
    {
        if (empty($_POST['aab034'])) {
            echo $this->buildResponse(400, [], '未获取市州代码');
            return;
        }
        $ori_params = $_POST;
        $ori_params['aab301'] = '';
        $ori_params['startrow'] = '1';
        $ori_params['endrow'] = '100';
        $result = $this->processOnRequest('Q7200', 'I01002', $ori_params);
        $content = $result['resultset'];
        echo $this->buildResponse(200, ['rows' => $content]);
    }

    /**
     * 2.2.10 社会保障卡制卡进度查询 | 社保查询-》社会保障卡查询-》社会保障卡制卡进度查询
     *
     * 正式环境测试通过
     */
    public function WZ2002()
    {
        $ori_params = [
            'startrow' => 1,
            'endrow' => 10,
        ];
        if (empty($_POST['aac002'])) {
            echo $this->buildResponse(400, [], '未获取到身份证信息');
            return;
        }
        if (isset($_POST['aac003'])) {
            $ori_params['aac003'] = trim($_POST['aac003']);
        }
        if (isset($_POST['startrow'])) {
            $ori_params['startrow'] = trim($_POST['startrow']);
        }
        if (isset($_POST['endrow'])) {
            $ori_params['endrow'] = trim($_POST['endrow']);
        }
        $result = $this->processOnRequest('WZ2002', 'I01002', $ori_params);
        echo $this->buildResponse(200, ['rows' => $result['resultset']]);
    }

    /**
     * 2.9.1.提交办件记录  业务系统通过此接口提交办件记录到好差评系统，根据评价方式和评价渠道选择推送公众号评价链接或者直接返回评价链接。
     */
    public function fwpjgl202SE008()
    {
        $mockProdParams = '{
    "publicKey": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCFQ1azNm7YF8GM/zdukuL9oHjso8yqd4UmwxYIIbI1NKsIKtE8U/GSGLsqkYrlWgjLC34PWBn1WA2LiPaDUY7kVn/4qNiahISrnnWXFoa07/bXjrEWVVSf5ZO+8NlimkLvUdZ1lBS6LA4FnDBlSBy7Z181gGizQvhKARqn41t9qQIDAQAB",
    "AAC375": "2029192",
    "AAZ400": "", 
    "BZE014": "wandawechat", 
    "BZE021":"2020-08-25 13:20:21",
    "YAZ001":"Wechat4158", 
    "AAA121": "20", 
    "AAA122": "公共服务", 
    "SERVICE_CODE":"00201400100Y",
    "SERVICE_NAME":"非关键信息变更",
    "SUBITEM_CODE":"51201074901402",
    "SUBITEM_NAME":"社会保障卡非关键信息变更",
    "proSource":"4", 
    "dataSource":"151", 
    "taskCode":"1251030069915888XY2512010749014", 
    "projectNo":"1251030069915888XY2512010749014202009170001" , 
    "projectType":"1", 
    "orderStatus":"5",  
    "acceptDept":"攀枝花市人力资源和社会保障信息中心", 
    "acceptCode":"1251030069915888XY", 
    "areaCode": "510400", 
    "OrgLevel": "3", 
    "operatorName":"攀枝花微信公众号",
    "userProp":"1",
    "userName": "李弘",
    "userCardNo": "510421199307205321",
    "userTel": "18982721837",
    "EVALUATE_CHANNL":"2", 
    "EVALUATE_TYPE":"2", 
}';
//        $mockItems = json_decode($mockProdParams, true);

        $mockDevelopParams = '{
    "publicKey": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDAqSfUpWzcvTc8KajPKtFDoHQEQREZuMl44sbmBL3n1Zvkt60nLCl3ITRVyxYL4wKyGq0CaYl5fT13TDhN1szY+DGVPFB1eggUP4NTUhtbzEKWkDqiNpxj9vdop2EZBLyrJIhcc5+VaLOFWIeqtCBYV+pm7AOQwrRfpZ2klt8COwIDAQAB",
    "AAC375": "2029192",
    "AAZ400": "", 
    "BZE014": "wandawechat", 
    "BZE021":"2020-08-25 13:20:21",
    "YAZ001":"Wechat4158", 
    "AAA121": "20", 
    "AAA122": "公共服务", 
    "SERVICE_CODE":"00201400100Y",
    "SERVICE_NAME":"非关键信息变更",
    "SUBITEM_CODE":"51201074901402",
    "SUBITEM_NAME":"社会保障卡非关键信息变更",
    "proSource":"4", 
    "dataSource":"151", 
    "taskCode":"1251030069915888XY2512010749014", 
    "projectNo":"1251030069915888XY2512010749014202009170001" , 
    "projectType":"1", // 
    "orderStatus":"5", // 
    "acceptDept":"攀枝花市人力资源和社会保障信息中心", 
    "acceptCode":"1251030069915888XY", 
    "areaCode": "510400", 
    "OrgLevel": "3", 
    "operatorName":"攀枝花微信公众号",
    "userProp":"1",
    "userName": "李弘",
    "userCardNo": "510421199307205321",
    "userTel": "18982721837",
    "EVALUATE_CHANNL":"2", 
    "EVALUATE_TYPE":"2", 
}';
        $mockItems = json_decode($mockDevelopParams, true);

        $result = $this->processOnRequest('fwpjgl202SE008', 'I01031', $mockItems);
        echo $this->buildResponse(200, ['rows' => $result['resultset']]);
    }

    public function doMergeData($cbList, $dnList)
    {
        $rebuildCbList = [];
        array_walk($cbList, function ($item) use (&$rebuildCbList) {
            $rebuildCbList[$item['AAE140']] = $item;
        });
        array_walk($dnList, function (&$item) use ($rebuildCbList) {
            if (isset($rebuildCbList[$item['AAE140']])) {
                $item = array_merge($item, $rebuildCbList[$item['AAE140']]);
            }
        });
        return $dnList;
    }


    public function processOnRequest($method, $callCode, $ori_params)
    {
        if ($callCode == self::SPECIAL_PARAMS) {
            $ori_params['jy_channel'] = self::JY_CHANNEL;
            $ori_params['jy_channel_city'] = self::JY_CHANNEL_CODE;
        }
        $code = json_encode($ori_params, JSON_UNESCAPED_UNICODE);
//        $this->logCommonFile($method, 'ORI_Params', $code);

        $params_encrypted = $this->getParamsRSA($code);
//        var_dump($params_encrypted);
        $content = $this->getSign($method, $callCode, $params_encrypted);
//        echo sprintf("request Params: %s \n", json_encode($content, JSON_UNESCAPED_UNICODE));
        $result = $this->restPost(self::$environmentConfig[self::ENVIRONMENT]['api_url'], $content);
        return $result;
    }

    protected function buildResponse($code = 0, $data = [], $message = "")
    {
        $response = [
            'code' => $code,
            'data' => $data,
            'message' => $code == 200 ? '业务授理成功' : '业务授理失败: ' . $message,
        ];
        return json_encode($response, JSON_UNESCAPED_UNICODE);
    }

    /**
     * 参数加密
     * @param string $data
     * @return string
     */
    protected function getParamsRSA($data)
    {
        $pubKey = "-----BEGIN PUBLIC KEY-----\n".wordwrap(self::$environmentConfig[self::ENVIRONMENT]['public_key'], 64, "\n", true)."\n-----END PUBLIC KEY-----";

//        $data = '{"aac002":"510302197606280045","jy_channel":"攀枝花微信公众平台","jy_channel_city":"510400"}';

//        $data = '{"jy_channel": "攀枝花微信公众平台","jy_channel_city": "510400","aac001": "3004737835","aae140": 110,"startrow": "1","endrow": "4"}';
//        var_dump($data);

        $split = str_split($data, self::MAX_ENCRYPT_BLOCK);
//        var_dump($split);

        $crypto = "";

        foreach ($split as $chunk) {
            openssl_public_encrypt($chunk, $encrypted, $pubKey);
            $crypto .= $encrypted;
//            $crypto .= base64_encode($encrypted);
        }
        $crypto = base64_encode($crypto);
//        echo sprintf("crypto : %s \n", $crypto);
        return $crypto;

    }

    public function restPost($url, $data){
        $ch = curl_init();
        $header = [
            'Content-Type:application/json',
            "Accept-Charset: utf-8"
        ];
        $requestParams = json_encode($data, JSON_UNESCAPED_UNICODE);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $requestParams);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        try {

            $curl_result = curl_exec($ch);
            $content = json_decode($curl_result, true);
            $logContent = sprintf("requestParams: %s ; responseContent: %s", $requestParams, $curl_result);
            $this->logCommonFile($data['method'], "RestApi_" . str_replace('/', '', $data['timestamp']), $logContent);
            if (intval($content['code']) != 0) {
                return $content;
            }

            return json_decode($content['data'], JSON_UNESCAPED_UNICODE);

        } catch (\Exception $e) {
            echo $e->getMessage();
            $this->logCommonFile(__FUNCTION__, 'Exception', $e->getMessage());
        }
    }

    /**
     * 公共请求参数数组排序再进行“Sha256WithRSA”加密之后得到的签名字符串作为sign的值，JSON字符串内的字均采用小写。
     * @param string $method
     * @param string $callCode
     * @param string $params
     * @return array
     */
    public function getSign($method, $callCode, $params)
    {
        $commonParams = [
            'sysCode' => self::$environmentConfig[self::ENVIRONMENT]['sys_code'],
            'channelName' => self::$environmentConfig[self::ENVIRONMENT]['channel_name'],
            'method' => $method,
            'callCode' => $callCode,
            'identityFlag' => 3,
            'timestamp' => date('Ymd/His/'),
            'params' => $params
        ];
        $content = $this->paramsSort($commonParams);
//        $this->logCommonFile($commonParams['method'], 'Sign', $content);
        $privateKey = "-----BEGIN RSA PRIVATE KEY-----\n".wordwrap(self::$environmentConfig[self::ENVIRONMENT]['private_key'], 64, "\n", true) . "\n-----END RSA PRIVATE KEY-----";
        openssl_sign($content, $signature, $privateKey);
        $sign = base64_encode($signature);
        $commonParams['sign'] = $sign;

        return $commonParams;
    }

    protected function paramsSort($content)
    {
        ksort($content);
        $buildString = [];
        array_walk($content, function ($value, $key) use (&$buildString, &$test) {
            if ($key == "callCode") {
                return;
            }
            $buildString[] = sprintf("%s=%s", $key, $value);
        });
        return implode("&", $buildString);
    }

    /**
     * 文件保存
     * @param string $action 方法名
     * @param string $type 类型(xml)
     * @param mixed $data 结果集
     */
    public function logCommonFile($action, $type, $data) {
        $dir = "storage/" . date("Y-m-d") . "/" . $action . "/";
        $filename = date("His") . "_" . $type . "_". rand(100, 10000);
        writeLog($dir, $filename, $data . "\n");
    }

    //生成 sha1WithRSA 签名
    public function genSign($toSign, $privateKey){
        $privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" .
            wordwrap($privateKey, 64, "\n", true) .
            "\n-----END RSA PRIVATE KEY-----";

        $key = openssl_get_privatekey($privateKey);
        openssl_sign($toSign, $signature, $key);
        openssl_free_key($key);
        $sign = base64_encode($signature);
        return $sign;
    }

    //校验 sha1WithRSA 签名
    public function verifySign($data, $sign, $pubKey){
        $sign = base64_decode($sign);

        $pubKey = "-----BEGIN PUBLIC KEY-----\n" .
            wordwrap($pubKey, 64, "\n", true) .
            "\n-----END PUBLIC KEY-----";

        $key = openssl_pkey_get_public($pubKey);
        $result = openssl_verify($data, $sign, $key, OPENSSL_ALGO_SHA1) === 1;
        return $result;
    }

    public function test()
    {
        $test = "callCode=I01020&channelName=攀枝花公共服务&method=CX016-JG&params=fHbq6qqtixFJWtN2ttUaqQvQjWdY25bBuYmXCdIShy2aH4/O6Vt1Bw7UYtalrAbYt+5QNXHkv5vjIS2ABEaijuUJ/bwzNb3xEYVc6tKmv3ZqitzXBll9QEK3m5ghT6n0eJYcmGnX9PftVkn87xyJz1h6yVWJ6dPj+1Rn36ZZNns=&sysCode=PzhGgfw&timestamp=20200805/140832/";
        $test = "channelName=天府通办&method=PRC-CX016&params=VP8xx3htY0uYPKWgmh7/pr0FrUOAiSuYGLp4CpSYbtJyQ4LP3YiVVmfGK0LptA2VG0eszaCRU4A34EnIlfMQHmwjqChysppbNHaex3kjfVGX8ljw9k60KW8WJu/e02Ws/FMxcUdC8KR1bTPaEqLQykQqtSKdDDea56FUZfiKmMSx4vzbVxYYb0FCPFAQ3Od3bDS0z12HuL5g+ZkDgtHcPCzNZ/XhuYgFBCR3aNDYYr+I0bowZmhZM40nx5zYoLcTXuxDtbgIvekm9w5amQVjxKviF1H+cCVJ282+09/vit3mlyPOVClGQPx1rHx9y4KhoDhmZRySdHFvo6q1uvN1bw==&sysCode=PzhGgfw&timestamp=20200805/145047/";
        $privateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAILFLdmFwfNHCGk3vpwj/FR9XpE9jjVreLlOaIEkSAOWOfzxCF7pMeSy0UDroRLgfLD4nQbUcK5AT7gg8PEM6H99SEHcpBkTKhfK1iI5iU9sN/WGL2Ft5T/uwo+KD7X3CsqMD0HsMQtv/YHJeAz/SPf5zyD5KttwohXXzO8a3P1pAgMBAAECgYAchxl2f6iNAu0Bzyhk9bDBWcw8kRop6zUd7836hkizh51E4ew6kFLTGnNt3zl3XcO187aF2+htCxiZCY6md3NstJod1zshoXf2slxxPWRUK92sS0XzlVT6ahVTdq2tAS3hOY3ldtnfOXMmMQPNn9OqlALQGOH/hJatBV6ZQxpp0QJBAMGKsxJkg3M6sZEO4zUgWgnGN+uS6nwAWpCfBY7eDvYSN78BxGGLgn+t40c0R8M/n85IM8OUmfHxTGWN3hYyGuUCQQCs+Kg1ypul9XLTWrVApyvZxY4ZxjAWG4yW4xqbUtHWUkGyle7Dy8xiYezl0kWN81lJNTz3qrJ4ZvM825+7tPw1AkBDPNxZV3ITZiCqNHHa0xJ0sthajv/HdJgCBjz4FU09T7buNL705HLByLdc1VzZCBGMqKjTGZ0h4KKZ5V9ydpXBAkABpZx0Zql0uxGM0aBILU9Nk4P22tw6WajNBvyJ3hABamcVvDe5xYb8qNIInifrYhXHjKo38XghjVljivPKZb7BAkAt4pCUYLI7LzLeSh/lnVxA7jOaRI3UsngCXn9VA4f+dkNcKAftRQqe7ytudqG07GKoInnDiyjrYe4kTLym8zoc";
        $pubKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCCxS3ZhcHzRwhpN76cI/xUfV6RPY41a3i5TmiBJEgDljn88Qhe6THkstFA66ES4Hyw+J0G1HCuQE+4IPDxDOh/fUhB3KQZEyoXytYiOYlPbDf1hi9hbeU/7sKPig+19wrKjA9B7DELb/2ByXgM/0j3+c8g+SrbcKIV18zvGtz9aQIDAQAB";

        $privateKey = self::$environmentConfig[self::ENVIRONMENT]['private_key'];
        $pubKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCsbrXx6hZl7Sh7iljjAXLPhX0Jfi8XQNcFybjqaBj56vvVRhIFZonRS0nGm4SpbnIzssHLQXn1fTuidQI9xX+vU209fBcxxhYbMQVfZ987V96sJ8AqkAVPZ+xFtnZRF1i2vsd2CVs/n79UlX5t1GQD89nNCz0EKUc8qi62VCcg+wIDAQAB";
//生成签名
        $sign = $this->genSign($test, $privateKey);

        echo sprintf("sign --> %s \n", $sign);
//校验签名
        $rs = $this->verifySign($test, $sign, $pubKey);
        var_dump($rs);
    }
}
